package com.emex.app.stuff.utils;

import java.awt.Frame;
import java.awt.Component;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ToolTipManager;
import org.openide.util.AsyncGUIJob;
import org.openide.windows.WindowManager;
import java.awt.EventQueue;
import javax.swing.JComboBox;
import javax.swing.JPopupMenu;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import org.openide.windows.TopComponent;

/**
 *
 * @author Ovoschnikova Elena
 */
public class UIUtils {

    private static Frame mainWindow = WindowManager.getDefault().getMainWindow();
    public static final int DELAY_DEFAULT_FOR_TOOL_TIP_TEXT = 5000;
    public static final int DELAY_LONG_FOR_TOOL_TIP_TEXT = 30000;
    public static final int LENGHT_FOR_TOOL_TIP_TEXT = 50;
    public static final String MODE_EDITOR = "editor";

    public static int showConfirmation(final String dialog) {
        return JOptionPane.showOptionDialog(mainWindow,
                dialog,
                "Подтверждение операции",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.INFORMATION_MESSAGE,
                null,
                new String[]{"Да", "Нет"},
                "default");
    }

    public static int showConfirmation(final Component component, final String dialog) {
        return JOptionPane.showOptionDialog(component,
                dialog,
                "Подтверждение операции",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.INFORMATION_MESSAGE,
                null,
                new String[]{"Да", "Нет"},
                "default");
    }

    public static int showDialogYesNoCancel(final String dialog, String[] buttonsText) {
        return JOptionPane.showOptionDialog(mainWindow,
                dialog,
                "Подтверждение операции",
                JOptionPane.YES_NO_CANCEL_OPTION,
                JOptionPane.INFORMATION_MESSAGE,
                null,
                buttonsText,
                "default");
    }

    public static int showDialogYesNoCancel(final Component component,
            final String dialog, String[] buttonsText) {
        return JOptionPane.showOptionDialog(component,
                dialog,
                "Подтверждение операции",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.INFORMATION_MESSAGE,
                null,
                buttonsText,
                "default");
    }

    public static void showErrorMessage(final String message) {
        JOptionPane.showMessageDialog(mainWindow,
                message, "Ошибка",
                JOptionPane.ERROR_MESSAGE);
    }

    public static void showErrorMessage(final Component component, final String message) {
        JOptionPane.showMessageDialog(component,
                message, "Ошибка",
                JOptionPane.ERROR_MESSAGE);
    }

    public static void showInfoMessage(final String message) {
        JOptionPane.showMessageDialog(mainWindow,
                message, "Информация",
                JOptionPane.INFORMATION_MESSAGE);
    }

    public static void showInfoMessage(final Component component, final String message) {
        JOptionPane.showMessageDialog(component,
                message, "Информация",
                JOptionPane.INFORMATION_MESSAGE);
    }

    public static void removeControlItemsFromTable(JTable table) {
        // Remove items for horizontal scrollbar, pack and packall from the table
        table.getActionMap().remove("column.horizontalScroll");
        table.getActionMap().remove("column.packAll");
        table.getActionMap().remove("column.packSelected");
    }

    public static void setClientPropertyDisabledForTopComponent(TopComponent tc) {
        tc.putClientProperty(TopComponent.PROP_CLOSING_DISABLED, Boolean.TRUE);
        tc.putClientProperty(TopComponent.PROP_DND_COPY_DISABLED, Boolean.TRUE);
        tc.putClientProperty(TopComponent.PROP_DRAGGING_DISABLED, Boolean.TRUE);
        tc.putClientProperty(TopComponent.PROP_UNDOCKING_DISABLED, Boolean.TRUE);
        tc.putClientProperty(TopComponent.PROP_SLIDING_DISABLED, Boolean.TRUE);
        tc.putClientProperty(TopComponent.PROP_MAXIMIZATION_DISABLED, Boolean.TRUE);
    }

    public static String textToMultiLineToolTipText(String text) {
        String[] words = text.split(" ");
        if (words.length == 0) {
            return "";
        }
        String newText = words[0];
        int k = 1;
        for (int i = 1; i < words.length; i++) {
            if (newText.length() + words[i].length() + 1
                    > LENGHT_FOR_TOOL_TIP_TEXT * k + 4 * (k - 1)) {
                newText += "<br>";
                newText += " " + words[i];
                k++;
            } else {
                newText += " " + words[i];
            }
        }
        newText = "<html>" + newText + "</html>";
        return newText;
    }

    public static void setDefaultDelayForToolTipText() {
        ToolTipManager.sharedInstance().setDismissDelay(DELAY_DEFAULT_FOR_TOOL_TIP_TEXT);
    }

    public static void setLongDelayForToolTipText() {
        ToolTipManager.sharedInstance().setDismissDelay(DELAY_LONG_FOR_TOOL_TIP_TEXT);
    }

    /**
     * Add horizontal scroll for combo box.
     * 
     * @param box combo box to add horizontal scroll
     */
    public static void adjustScrollBarForComboBox(JComboBox box) {
        if (box.getItemCount() == 0) {
            return;
        }
        Object comp = box.getUI().getAccessibleChild(box, 0);
        if (!(comp instanceof JPopupMenu)) {
            return;
        }
        JPopupMenu popup = (JPopupMenu) comp;
        JScrollPane scrollPane = (JScrollPane) popup.getComponent(0);
        scrollPane.setHorizontalScrollBar(new JScrollBar(JScrollBar.HORIZONTAL));
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    }
    
    public static void notifyOperationNotAllowed() {
        showInfoMessage("У вас недостаточно прав на выполнение данной операции");
    }
}
